package com.study.springsecurity.pojo;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

import java.util.Collection;

/**
 * 为了保存用户额外信息，继承User
 *
 * @Author YL
 * @Create 2022/10/21 23:16
 * @Version 1.0
 */
@Getter
@Setter
@EqualsAndHashCode
public class MyUserDetails extends User {

    /*用户id*/
    private Long userId;

    public MyUserDetails(Long userId, String username, String password, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
        this.userId = userId;
    }

    public MyUserDetails(Long userId, String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        this.userId = userId;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof MyUserDetails) {
            return this.userId.longValue() == ((MyUserDetails) obj).userId.longValue() && this.getUsername().equals(((User) obj).getUsername());
        }
        return false;
    }

    @Override
    public int hashCode() {
        return this.userId.hashCode();
    }
}
